package se.perp.c4.ui;

import se.perp.c4.control.iface.IMenuListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;
import javax.swing.JCheckBoxMenuItem;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.KeyStroke;
import se.perp.c4.control.iface.IReportCreator;
import se.perp.c4.control.iface.ISpecialCardWarningOwner;
import se.perp.c4.control.iface.IUIStateListener;

public class MainMenu extends JMenuBar implements ActionListener, ISpecialCardWarningOwner {

    private JMenu game,  option,  help;
    private JMenuItem gameAsServer,  gameAsClient,  gameAgainstBot,  gameGetReport,  gameExit;
    private JMenuItem optionPlaySounds,  optionPopups,  optionCenteredMsg,  optionSpecialCardReminder,  optionUseHighlight,  helpAbout;
    private IMenuListener menuListener = null;
    private IUIStateListener uiStateListener = null;
    private IReportCreator reportCreator = null;
    private final Properties properties;

    public MainMenu(Properties properties) {
        this.properties = properties;

        game = new JMenu("Game");
        game.setMnemonic('G');

        gameAsServer = new JMenuItem("Play as server", 'P');
        gameAsServer.setAccelerator(KeyStroke.getKeyStroke("F2"));
        gameAsServer.addActionListener(this);
        game.add(gameAsServer);

        gameAsClient = new JMenuItem("Play as client", 'c');
        gameAsClient.setAccelerator(KeyStroke.getKeyStroke("F4"));
        gameAsClient.addActionListener(this);
        game.add(gameAsClient);

//        gameAgainstBot = new JMenuItem("Play against bot", 'b');
//        gameAgainstBot.setAccelerator(KeyStroke.getKeyStroke("control B"));
//        gameAgainstBot.addActionListener(this);
//        game.add(gameAgainstBot);

        game.addSeparator();

        gameGetReport = new JMenuItem("View remaining enemies", 'r');
        gameGetReport.setAccelerator(KeyStroke.getKeyStroke("control R"));
        gameGetReport.addActionListener(this);
        gameGetReport.setEnabled(false);
        game.add(gameGetReport);

        game.addSeparator();

        gameExit = new JMenuItem("Exit", 'x');
        gameExit.setAccelerator(KeyStroke.getKeyStroke("alt F4"));
        gameExit.addActionListener(this);
        game.add(gameExit);

        add(game);

        option = new JMenu("Option");
        option.setMnemonic('O');

        optionPlaySounds = new JCheckBoxMenuItem("Play sounds");
        optionPlaySounds.setMnemonic('s');
        optionPlaySounds.setAccelerator(KeyStroke.getKeyStroke("control S"));
        optionPlaySounds.addActionListener(this);
        optionPlaySounds.setSelected(Boolean.parseBoolean(
                properties.getProperty("sound")));
        option.add(optionPlaySounds);

        optionPopups = new JCheckBoxMenuItem("\"Your turn\" popup alerts");
        optionPopups.setMnemonic('Y');
        optionPopups.setAccelerator(KeyStroke.getKeyStroke("control P"));
        optionPopups.addActionListener(this);
        optionPopups.setSelected(Boolean.parseBoolean(
                properties.getProperty("popup")));
        option.add(optionPopups);

        optionCenteredMsg = new JCheckBoxMenuItem("Centered wait message");
        optionCenteredMsg.setMnemonic('C');
        optionCenteredMsg.setAccelerator(KeyStroke.getKeyStroke("control M"));
        optionCenteredMsg.addActionListener(this);
        optionCenteredMsg.setSelected(Boolean.parseBoolean(
                properties.getProperty("centermsg")));
        option.add(optionCenteredMsg);

        option.addSeparator();

        optionSpecialCardReminder = new JCheckBoxMenuItem("Confirm when playing special cards");
        optionSpecialCardReminder.setMnemonic('A');
        optionSpecialCardReminder.setAccelerator(KeyStroke.getKeyStroke("control L"));
        optionSpecialCardReminder.addActionListener(this);
        optionSpecialCardReminder.setSelected(Boolean.parseBoolean(
                properties.getProperty("specialcardreminder")));
        option.add(optionSpecialCardReminder);

        optionUseHighlight = new JCheckBoxMenuItem("Use movement assist highlighting");
        optionUseHighlight.setMnemonic('h');
        optionUseHighlight.setAccelerator(KeyStroke.getKeyStroke("control H"));
        optionUseHighlight.addActionListener(this);
        optionUseHighlight.setSelected(Boolean.parseBoolean(
                properties.getProperty("movementassist")));
        option.add(optionUseHighlight);

        add(option);

        help = new JMenu("Help");
        help.setMnemonic('H');

        helpAbout = new JMenuItem("About", 'b');
        helpAbout.setAccelerator(KeyStroke.getKeyStroke("F1"));
        helpAbout.addActionListener(this);
        help.add(helpAbout);

        add(help);
    }

    public boolean getPlaySounds() {
        return optionPlaySounds.isSelected();
    }

    public boolean getShowCenteredMsg() {
        return optionCenteredMsg.isSelected();
    }

    public boolean getShowPopups() {
        return optionPopups.isSelected();
    }

    public boolean getUseHighlight() {
        return optionUseHighlight.isSelected();
    }

    public boolean getWarnAboutSpecialCards() {
        return optionSpecialCardReminder.isSelected();
    }

    public void setGameAsServerEnabled(boolean enabled) {
        gameAsServer.setEnabled(enabled);
    }

    public void setGameAsClientEnabled(boolean enabled) {
        gameAsClient.setEnabled(enabled);
    }

    public void setGameGetReportEnabled(boolean enabled) {
        gameGetReport.setEnabled(enabled);
    }

    public void setMenuListener(IMenuListener l) {
        menuListener = l;
    }

    public void setUIStateListener(IUIStateListener l) {
        uiStateListener = l;
    }

    public void setReportCreator(IReportCreator l) {
        reportCreator = l;
    }

    public void actionPerformed(ActionEvent e) {
        if (menuListener == null) {
            return;
        }

        JMenuItem item = (JMenuItem) e.getSource();
        if (item == gameExit) {
            menuListener.gameExit();
        } else if (item == helpAbout) {
            menuListener.helpAbout();
        } else if (item == gameAsServer) {
            menuListener.gameAsServer(this);
        } else if (item == gameAsClient) {
            menuListener.gameAsClient(this);
        } else if (item == gameAgainstBot) {
//            menuListener.gameAgainstBot(game);
        } else if (item == gameGetReport) {
            if (reportCreator != null) {
                reportCreator.showReport();
            }
        } else if (item == optionPlaySounds) {
            properties.setProperty("sound", String.valueOf(optionPlaySounds.isSelected()));

            if (uiStateListener != null) {
                uiStateListener.appStateChange_sound(optionPlaySounds.isSelected());
            }
        } else if (item == optionPopups) {
            properties.setProperty("popup", String.valueOf(optionPlaySounds.isSelected()));

            if (uiStateListener != null) {
                uiStateListener.appStateChange_popup(optionPopups.isSelected());
            }
        } else if (item == optionCenteredMsg) {
            properties.setProperty("centermsg", String.valueOf(optionCenteredMsg.isSelected()));

            if (uiStateListener != null) {
                uiStateListener.appStateChange_centerMsg(optionCenteredMsg.isSelected());
            }
        } else if (item == optionSpecialCardReminder) {
            properties.setProperty("specialcardreminder", String.valueOf(optionSpecialCardReminder.isSelected()));
        } else if (item == optionUseHighlight) {
            properties.setProperty("movementassist", String.valueOf(optionUseHighlight.isSelected()));
            if (uiStateListener != null) {
                uiStateListener.appStateChange_useHighlight(optionUseHighlight.isSelected());
            }
        }
    }
}
